home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / misc / football / exec / league.rexx < prev    next >
OS/2 REXX Batch file  |  1999-11-29  |  16KB  |  465 lines

  1. /* ***********************************************************************
  2.  
  3.    LEAGUE PROGRAM FOR FOOTBALL REXX SUITE
  4.   ----------------------------------------
  5.                    Copyright  Mark Naughton 1996
  6.  
  7.  
  8. Version    Date     History
  9. --------------------------------------------------------------------------
  10.  1.0       090996   First release.
  11.            090996   As the sort doesnt flip the negative numbers, was
  12.                     thought to be a problem, the league is checked and
  13.                     flipped correctly (LEAGUE2.REXX)
  14.            120996   Amended same code as it didn't work if the two 'signs'
  15.                     were different. Now flips them correctly.
  16.                     (LEAGUE2.REXX)
  17.            180996   Changed record-count so that it is stored in element
  18.                     zero of each array. Deleted some variable assigns
  19.                     that weren't needed. Amended reading into arrays so
  20.                     that the count is set correctly for one element.
  21.                     Removed all "call "references before library routines.
  22.            180996   Removed all "call " references, used element zero to
  23.                     store the record-count. (LEAGUE2.REXX)
  24.            260996   Amended program so that the sortprogram and the REXX
  25.                     program to display the league are called from here
  26.                     instead of a separate script file. Changed so that the
  27.                     count is not stored in 'zero' element.
  28.            270996   Added code for points_per_draw and points_per_loss.
  29.                     Updated to use new filenames. Improved error messages.
  30.                     Inserted code from LEAGUE2.REXX to speed things up and
  31.                     to tidy the code. Finally got the table to be sorted
  32.                     properly, using a version of VSORTA from asd_library.
  33.  1.1       111196   Added argument so it can become a component of
  34.                     FOOTBALL. Removed GAMEPLAY and EXTRACT. Removed all
  35.                     messages.
  36.            131196   Added checks for files - if not found, exits without
  37.                     a message.
  38.            141196   Added code to highlight the leader.
  39.            201196   Added code to sort out which teams are relegated and
  40.                     then to display the line at the correct place.
  41.            211196   Updated and tidied the display.
  42.            231296   Shortened lines when displaying the league.
  43.            241296   Tidied up display again.
  44.  1.2       110497   Added code to support Points_Per_Goals. Amended the
  45.                     table display.
  46.            070597   Amended code that checks league_file to see if they
  47.                     are in the right order - was using wrong parameters.
  48.            080597   Amended as FOOTSORT is only used for league table file
  49.                     sorting.
  50.  2.0       240997   Tidied up code and added errors in case any files cannot
  51.                     be read or written to. Simplified reading of teams and
  52.                     data (getting it from '.df' now, not '.stats') and
  53.                     getting the total teams without having to re-read the
  54.                     '.stats' file. Added promotion bar.
  55.            151297   Tidied display.
  56.            030599   Fixed bug in relegation bar - it would show it at the
  57.                     maximum position for the number of teams relegated but
  58.                     this wasn't true when a team was relegated and the
  59.                     others still had to fight it out - same for the
  60.                     promotion bar.
  61.            050599   Okay, I decided to change the display again.
  62.            170599   Fixed bug in display of relegated teams as they all
  63.                     showed in upper case when 2 hadn't had the misfortune.
  64.                     Fixed bug reported by Kev (Thanks!) where Bolton from
  65.                     the 97/98 English Premiership weren't upper-cased -
  66.                     traced to the fact that they had the same points as
  67.                     the place above - changed so calculation is changed if
  68.                     its the last game of the season.
  69.            170699   Fixed bug where if all teams had zero points, the
  70.                     promotion bar was still displayed.
  71.            250899   Added error msg to file checks.
  72.            270899   Converted to use locale. Some error messages, before
  73.                     reading the locale, will still be in English.
  74.  
  75.  
  76. **************************************************************************
  77.  
  78. Procedure
  79. ---------
  80.  
  81. 1. Check files exist. Open 'Teams.df'.
  82. 2. Read in teams and their associated data; WIN, DRAW, LOST etc.
  83. 3. Close file. Open 'Teams.stats'. Read data. Close file.
  84. 4. Open 'Teams.sf' for reading.
  85. 5. Read each line that has a game played, and from that get the team names
  86.    and the score. Then update the relevant team data with the result.
  87. 6. Write data in form of a league table to an output file.
  88. 7. Close file.
  89. 8. Call external sortprogram to sort the output file.
  90. 9. Open 'Teams.stats' and read the number of teams, then close.
  91. 10.Open 'League.output' and read contents into two arrays.
  92. 11.Check to see if the teams are in the right order, comparing the points
  93.    of each one with the next and if the goal-differences are in the wrong
  94.    order, then swap the two teams.
  95. 12.Check to see which teams are relegated by calculating the maximum number
  96.    of points available and seeing if the lower teams are able to move up.
  97.    If not they are relegated, according to the number set.
  98. 13.Check to see which teams are promoted by using the same method as above.
  99. 14.Display league and exit.
  100.  
  101. ************************************************************************** */
  102. parse arg league_file
  103.  
  104. version      = 2
  105. league_file  = "Data/" || league_file
  106. input_file   = '.stats'
  107. input2_file  = '.sf'
  108. input3_file  = '.df'
  109. output_file  = 'Data/League.output'
  110. title        = '*LEAGUE_NAME='
  111. points_win   = '*POINTS_PER_WIN='
  112. points_drw   = '*POINTS_PER_DRW='
  113. points_lse   = '*POINTS_PER_LSE='
  114. points_gls   = '*POINTS_PER_GLS='
  115. releg        = '*RELEGATION='
  116. playother    = '*PLAY_OTHER='
  117. promoted     = '*PROMOTED='
  118. team_counter = '*COUNTR='
  119. team         = '*TEAM='
  120. played       = '*PLY='
  121. won          = '*WIN='
  122. drawn        = '*DRW='
  123. lost         = '*LST='
  124. goalsf       = '*GOF='
  125. goalsa       = '*GOA='
  126. points       = '*PTS='
  127. separator    = '*'
  128. teams.       = '???'
  129. teams2.      = '???'
  130. m_ply.       = '???'
  131. m_win.       = '???'
  132. m_drw.       = '???'
  133. m_lost.      = '???'
  134. m_gof.       = '???'
  135. m_goa.       = '???'
  136. m_pts.       = '???'
  137. teams_ctr    = 0
  138. playo        = 2
  139. reg          = 2
  140. ptsgls       = 0
  141. ptswin       = 2
  142. ptsdrw       = 1
  143. ptslse       = 0
  144. promo        = 0
  145. not_played   = '__   __'
  146.  
  147.  
  148. if open(datafile,"Data/Football.locale",'r') then do
  149.    line = readln(datafile)
  150.    locdir = strip(line)
  151.    close(datafile)
  152. end
  153. else do
  154.    say
  155.    say "ERROR :    (League)"
  156.    say
  157.    say "Cannot read 'Data/Football.locale' for the locale settings."
  158.    exit
  159. end
  160.  
  161. locdir = locdir"Exec/League.data"
  162.  
  163. if open(datafile,"ENV:FootballRXPath",'r') then do
  164.    line = readln(datafile)
  165.    rxdir = strip(line)
  166.    close(datafile)
  167. end
  168. else
  169.    rxdir = "SYS:Rexxc/"
  170.  
  171. if exists(locdir) > 0 then do
  172.   address command rxdir'rx 'locdir
  173.   VarCount = getclip('VarCount')
  174.   do i = 1 to VarCount
  175.     interpret getclip('var.'i)
  176.   end
  177. end
  178. else do
  179.    say
  180.    say "ERROR :    (League)"
  181.    say
  182.    say "Cannot find '"locdir"' to read locale settings."
  183.    exit
  184. end
  185.  
  186. if exists(league_file || input_file) = 0  then do
  187.    say
  188.    say lg_error
  189.    say
  190.    say lg_one"'"league_file||input_file"'."
  191.    exit
  192. end
  193. if exists(league_file || input2_file) = 0 then do
  194.    say
  195.    say lg_error
  196.    say
  197.    say lg_one"'"league_file||input2_file"'."
  198.    exit
  199. end
  200. if exists(league_file || input3_file) = 0 then do
  201.    say
  202.    say lg_error
  203.    say
  204.    say lg_one"'"league_file||input3_file"'."
  205.    exit
  206. end
  207.  
  208. tcount = 0
  209. if open(datafile,league_file || input3_file,'r') then do
  210.    do while ~eof(datafile)
  211.       line = readln(datafile)
  212.       if pos(title,line) > 0 then        league_title = delstr(line,1,13)
  213.       if pos(points_win,line) > 0 then   ptswin=delstr(line,1,16)
  214.       if pos(points_drw,line) > 0 then   ptsdrw=delstr(line,1,16)
  215.       if pos(points_lse,line) > 0 then   ptslse=delstr(line,1,16)
  216.       if pos(points_gls,line) > 0 then   ptsgls=delstr(line,1,16)
  217.       if pos(releg,line) > 0 then        reg = delstr(line,1,12)
  218.       if pos(playother,line) > 0 then    playo = delstr(line,1,12)
  219.       if pos(promoted,line) > 0 then     promo = delstr(line,1,10)
  220.       if pos(separator,line) = 0 & line ~="" then do
  221.          line = strip(line)
  222.          tcount       = tcount + 1
  223.          teams.tcount = line
  224.       end
  225.    end
  226.    close(datafile)
  227. end
  228. else do
  229.    say
  230.    say lg_error
  231.    say
  232.    say lg_two"'"league_file || input3_file"'."
  233.    exit
  234. end
  235.  
  236. if open(datafile,league_file || input_file,'r') then do
  237.    do while ~eof(datafile)
  238.       line = readln(datafile)
  239.       if pos(team,line) > 0 then
  240.          teams_ctr = teams_ctr + 1
  241.       if pos(played,line) > 0 then  m_ply.teams_ctr  = delstr(line,1,5)
  242.       if pos(won,line) > 0 then     m_won.teams_ctr  = delstr(line,1,5)
  243.       if pos(drawn,line) > 0 then   m_drw.teams_ctr  = delstr(line,1,5)
  244.       if pos(lost,line) > 0 then    m_lost.teams_ctr = delstr(line,1,5)
  245.       if pos(goalsf,line) > 0 then  m_gof.teams_ctr  = delstr(line,1,5)
  246.       if pos(goalsa,line) > 0 then  m_goa.teams_ctr  = delstr(line,1,5)
  247.       if pos(points,line) > 0 then  m_pts.teams_ctr  = delstr(line,1,5)
  248.    end
  249.    close(datafile)
  250. end
  251. else do
  252.    say
  253.    say lg_error
  254.    say
  255.    say lg_two"'"league_file || input_file"'."
  256.    exit
  257. end
  258.  
  259. if open(datafile,league_file || input2_file,'r') then do
  260.    do while ~eof(datafile)
  261.       line = readln(datafile)
  262.       if pos(separator,line) = 0 then do
  263.          if pos(not_played,line) = 0 then do
  264.             home_team = strip(substr(line,1,30))
  265.             goals_for = substr(line,32,2)
  266.             goals_aga = substr(line,37,2)
  267.             away_team = strip(substr(line,41,30))
  268.  
  269.             do i=1 to teams_ctr
  270.                if home_team == teams.i then do
  271.                   m_ply.i = m_ply.i + 1
  272.                   m_gof.i = m_gof.i + goals_for
  273.                   m_goa.i = m_goa.i + goals_aga
  274.                   if goals_for = goals_aga then do
  275.                      m_drw.i = m_drw.i + 1
  276.                      m_pts.i = m_pts.i + ptsdrw
  277.                   end
  278.                   if goals_for < goals_aga then do
  279.                      m_lost.i= m_lost.i + 1
  280.                      m_pts.i = m_pts.i + ptslse
  281.                   end
  282.                   if goals_for > goals_aga then do
  283.                      m_won.i = m_won.i + 1
  284.                      m_pts.i = m_pts.i + ptswin
  285.                   end
  286.                   m_pts.i = m_pts.i + (goals_for * ptsgls)
  287.                end
  288.             end
  289.             do i=1 to teams_ctr
  290.                if away_team == teams.i then do
  291.                   m_ply.i = m_ply.i + 1
  292.                   m_gof.i = m_gof.i + goals_aga
  293.                   m_goa.i = m_goa.i + goals_for
  294.                   if goals_for = goals_aga then do
  295.                      m_drw.i = m_drw.i + 1
  296.                      m_pts.i = m_pts.i + ptsdrw
  297.                   end
  298.                   if goals_for < goals_aga then do
  299.                      m_won.i = m_won.i + 1
  300.                      m_pts.i = m_pts.i + ptswin
  301.                   end
  302.                   if goals_for > goals_aga then do
  303.                      m_lost.i= m_lost.i + 1
  304.                      m_pts.i = m_pts.i + ptslse
  305.                   end
  306.                   m_pts.i = m_pts.i + (goals_aga * ptsgls)
  307.                end
  308.             end
  309.          end
  310.       end
  311.    end
  312.    close(datafile)
  313. end
  314. else do
  315.    say
  316.    say lg_error
  317.    say
  318.    say lg_two"'"league_file || input2_file"'."
  319.    exit
  320. end
  321.  
  322. if open(outfile,output_file,"w") then do
  323.    do i=1 to teams_ctr
  324.       line = teams.i
  325.       line = insert(" ",line,length(line)+1,30-length(line))
  326.       mp = right(m_ply.i,3)
  327.       mw = right(m_won.i,3)
  328.       md = right(m_drw.i,3)
  329.       ml = right(m_lost.i,3)
  330.       mgf = right(m_gof.i,5)
  331.       mga = right(m_goa.i,5)
  332.       mpts = right(m_pts.i,7)
  333.       writech(outfile,line"   "mp" "mw" "md" "ml" "mgf" "mga"  "mpts"      ")
  334.       itemp=mgf-mga
  335.       if itemp>0 then
  336.          itemp=insert("+",itemp,0,1)
  337.       itemp=right(itemp,4)
  338.       writeln(outfile,itemp)
  339.    end
  340.    close(outfile)
  341. end
  342. else do
  343.    say
  344.    say lg_error
  345.    say
  346.    say lg_three"'"output_file"'."
  347.    exit
  348. end
  349.  
  350. address command 'Exec/footsort '
  351.  
  352. if open(datafile2,output_file,'r') then do
  353.    do i=1 to tcount
  354.       line = readln(datafile2)
  355.       teams.i  = line
  356.       teams2.i = line
  357.    end
  358.    close(datafile2)
  359. end
  360. else do
  361.    say
  362.    say lg_error
  363.    say
  364.    say lg_four"'"output_file"'."
  365.    exit
  366. end
  367.  
  368. ctr = 0
  369. do while swapctr > 0
  370.    swapctr=9
  371.    do i=1 to tcount-1                               /* was 64,4 and 77,4 */
  372.       swapdone=0
  373.       j = i + 1
  374.       if strip(substr(teams.i,64,7)) = strip(substr(teams.j,64,7)) then do
  375.          goaldiffa = strip(substr(teams.i,77,5))
  376.          goaldiffb = strip(substr(teams.j,77,5))
  377.          if goaldiffa < goaldiffb then do
  378.             teams.i = teams2.j
  379.             teams.j = teams2.i
  380.             teams2.i= teams.i
  381.             teams2.j= teams.j
  382.             swapdone= 1
  383.             ctr = ctr + 1
  384.          end
  385.       end
  386.       if swapdone = 1 then
  387.          break
  388.    end
  389.    if ctr = 0 then
  390.       swapctr = 0
  391.    else
  392.       ctr = 0
  393. end
  394.  
  395. posreg   = 0
  396. pospro   = 0
  397. if reg > 0 then do             /* this bit of code checks to see which teams are relegated */
  398.    wherereg = tcount - reg
  399.    regpts   = strip(substr(teams.wherereg,64,7))         /* was 64,4 */
  400.    do i=tcount to 1 by -1
  401.       b = (((tcount-1) * playo) - strip(substr(teams.i,35,3))) * ptswin  /* was 36,2 */
  402.       c = strip(substr(teams.i,64,7)) + b
  403.       if c < regpts then posreg = i                 /* calculates the max points avail */
  404.       if c = regpts & strip(substr(teams.i,35,3)) = ((tcount-1) * playo) then
  405.          posreg = i
  406.    end
  407.    if posreg < (wherereg+1) & posreg ~= 0 then      /* was 0 - set to reqd pos if higher */
  408.       posreg = wherereg + 1
  409.                                                     /* Bug fix 0305 - relegation bar problems */
  410. end
  411.  
  412. /* ---------------------------------------------- */
  413.  
  414. if promo > 0 then do          /* this bit of code checks to see which teams are promoted */
  415.    wherepro = promo + 1
  416.    propts   = strip(substr(teams.wherepro,64,7))         /* was 64,4 */
  417.    do i=1 to tcount
  418.       if strip(substr(teams.i,35,3)) > 0 then
  419.          b = (((tcount-1) * playo) - strip(substr(teams.i,35,3))) * ptswin
  420.       else
  421.          b = 0
  422.       c = strip(substr(teams.i,64,7)) + b
  423.       if c > propts then pospro = i                 /* calculates the max points avail */
  424.    end
  425.    if pospro > (wherepro-1) & pospro ~= 0 then
  426.       pospro = wherepro - 1                         /* was 0 - set to reqd pos if lower */
  427. end
  428.  
  429. /* ---------------------------------------------- */
  430.  
  431. say
  432. say center(league_title,88)
  433. say "-----------------------------------------------------------------------------------------"
  434. say
  435. say
  436. say lg_five
  437. say "-----------------------------------------------------------------------------------------"
  438.  
  439. do i=1 to tcount
  440.    if strip(substr(teams.i,76,5)) = 0 then teams.i=overlay("",teams.i,76,5," ")
  441.    if i >= posreg & posreg~=0 then teams.i = upper(teams.i)
  442.    if i<10 then do
  443.       if i = 1 then teams.i = upper(teams.i)
  444.       say " "i") "teams.i
  445.    end
  446.    else
  447.       say i") "teams.i
  448.    if reg ~= 0 then do
  449.       if i = (tcount-reg) & posreg~=0 then do       /* was posreg */
  450.          say "_________________________________________________________________________________________"
  451.          say
  452.       end
  453.    end
  454.    if promo ~= 0 then do
  455.       if i = (promo) & pospro~=0 then do        /* was pospro */
  456.          say "_________________________________________________________________________________________"
  457.          say
  458.       end
  459.    end
  460. end
  461.  
  462. say "-----------------------------------------------------------------------------------------"
  463. say
  464.  
  465. exit